package com.liuhongwei.test.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.util.List;

// 自定义属性生成规则
public class CustomNamingPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    // 自定义字段生成规则
    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass,
                                       IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable,
                                       ModelClassType modelClassType) {
        // 获取默认生成的属性名
        String originalName = field.getName();
        // 自定义转换规则
        String customName = convertUnderscoreName(originalName);
        field.setName(customName);
        return true;
    }

    // 自定义getter方法生成规则
    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable,
                                              ModelClassType modelClassType) {
        String propertyName = introspectedColumn.getJavaProperty();
        String customName = convertUnderscoreName(propertyName);
        method.setName("get" + capitalize(customName));
        return true;
    }

    // 自定义setter方法生成规则
    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass,
                                              IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable,
                                              ModelClassType modelClassType) {
        String propertyName = introspectedColumn.getJavaProperty();
        String customName = convertUnderscoreName(propertyName);
        method.setName("set" + capitalize(customName));
        return true;
    }

    // 将下划线命名转换为驼峰命名（保留原始大小写）
    private String convertUnderscoreName(String name) {
        if (name == null || name.isEmpty()) {
            return name;
        }
//        // 特殊处理以p_开头的情况
//        if (name.startsWith("p_") && name.length() > 2) {
//            return "p" + name.substring(2);
//        }
        // 常规驼峰转换
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = false;
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    result.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    result.append(c);
                }
            }
        }
        return result.toString();
    }

    // 首字母大写
    private String capitalize(String s) {
        if (s == null || s.isEmpty()) {
            return s;
        }
        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }
}